package com.hl.sfzy.controller.content;

import com.hl.sfzy.utils.FileUpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.content.Article;
import com.hl.sfzy.service.content.ArticleService;
import com.hl.sfzy.utils.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @className: ArticleController
 * @description: 文章控制器
 * @param: articleService 文章服务
 * @return: ArticleController
 * @version: v0.1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    public ArticleService articleService;


    @Value("${article.banner.img.dir.path}")
    private String banner_url;

    /**
     * @apiNote: 获取所有文章
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return R<PageInfo<Article>>
     */
    @GetMapping("/getAllArticles")
    public R<PageInfo<Article>> getMethodName(Integer pageNum, Integer pageSize) {
        PageInfo<Article> articles = articleService.getArticles(pageNum, pageSize);
        return R.success(articles);
    }

    /**
     * @apiNote: 添加文章
     * @param article 文章
     * @return R<Integer>
     */
    @PostMapping("/addArticle")
    public R<Integer> addArticle(@RequestBody Article article) {
        article.createTime = LocalDateTime.now();
        article.lastUpdateTime = LocalDateTime.now();article.setStatus(1);

        int i = articleService.addArticle(article);


        return i > 0 ? R.success(i) : R.failure("添加失败");
    }

    /**
     * @apiNote: 删除文章
     * @param id 文章id
     * @return R<Integer>
     */
    @DeleteMapping("/removeArticle/{id}")
    public R<Integer> removeArticle(@PathVariable("id") Integer id) {
        int i = articleService.removeArticle(id);
        return i > 0 ? R.success(i) : R.failure("删除失败");
    }

    /**
     * @apiNote: 根据id获取文章用于回显
     * @param id 文章id
     * @return R<Article>
     */
    @GetMapping("/getArticleById")
    public R<Article> getArticleById(Long id) {
        Article articleById = articleService.getArticleById(id);
        return Optional.ofNullable(articleById).map(R::success).orElseGet(() -> R.failure("查询失败"));
    }

    /**
     * @apiNote: 搜索文章
     * @param key 关键字
     * @return R<List<Article>>
     */
    @GetMapping("/searchArticles")
    public R<PageInfo<Article>> searchArticles(@RequestParam("q") String key,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime starTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        PageInfo<Article> pageInfo = articleService.searchArticles(key, starTime, endTime, pageNum, pageSize);
        return Optional.ofNullable(pageInfo).map(R::success).orElseGet(() -> R.failure("查询失败"));
    }



    /**
     * @apiNote: 更新文章
     * @param article 文章
     * @return R<Integer>
     */
    @PutMapping("/updateArticle")
    public R<Integer> updateArticle(@RequestBody Article article) {
        int i = articleService.updateArticle(article);
        return i > 0 ? R.success(i) : R.failure("更新失败");
    }



    @PostMapping("/fileup")
    public R<String> articleImgUpload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()){
            return R.failure("文件为空");
        }
        String fileName = FileUpUtils.fileUp(file, banner_url);
        return R.success(banner_url +"/"+fileName);
    }


    @GetMapping("down")
    public void down(HttpServletResponse response, String fileName) throws IOException {
        // 原生servlet
        File file = new File(fileName);
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(inputStream, outputStream);
    }

}
